<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir(dirname(__FILE__));

include_once $path."/../lib/core.class.php";


class SiblingSnippet extends IPlugin 
{
    function siblingSnippet($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
    }

    function getPluginTag() { return "sibling"; }
    function getPluginName() { return "Create Sibling of defined items list"; }
    function getPluginVersion() { return "1.0.0"; }

    /*
    *   Handle plugin actions
    *
    */
    function doAction($params = null)
    {
        $result = null;

        if( null != $params && is_array($this->childs) && isset($this->childs[$params[0]]))
        {
            $id = $params[0];
            unset($params[0]);
            $params = ArrayReIndex($params);
            $result = $this->childs[$id]->doAction($params);
        }
        if("removeWrongSibling" == $params['action'])
        {

            $sTable     = $params['table'];
            $sField     = $params['sField'];
            $sId        = $params['sId'];
            $sParentId  = $params['sParentId'];
            $sWhere     = $params['sWhere'];
            $sOrderBy   = $params['sOrderBy'];
            if( isne($sWhere) )
                $sWhere .=" and ";
            
            $core = getCoreInstance();
            $db = $core->getProperty("db");
            // remove sibling = -1
            $LeftsParentId = ($sParentId == "") ? "" : ", " . $sParentId;
            $q = "select {$sId} {$LeftsParentId} from {$sTable} where {$sWhere} {$sField}<=0";
            $minus_res = $db->getAll($q);
            
            for($i = 0 ; $i < sizeof($minus_res) ; $i++){
                if(is_null($minus_res[$i][$sParentId]) or !isset($minus_res[$i][$sParentId])){
                    $LeftsParentId = ($sParentId == "") ? " 1=1 " : $sParentId . " is null ";    
                }else{
                    $LeftsParentId = ($sParentId == "") ? " 1=1 " : $sParentId . "=" . $minus_res[$i][$sParentId] . " ";
                }
                $max_sibling_in_folder = "select max({$sField}) from {$sTable} where  {$sWhere} {$LeftsParentId} {$sOrderBy}";
                $max_sibling_in_folder = $db->getOne($max_sibling_in_folder);
                if(-1==$max_sibling_in_folder)
                    $max_sibling_in_folder = 0;
                $max_sibling_in_folder++;
                $upd_sibling = "update {$sTable} set {$sField}='{$max_sibling_in_folder}' where {$sId}='{$minus_res[$i][$sId]}'";
                $db->exec($upd_sibling);
            }
            // end of sibling -1 
        }
        return $result;
    }
    
    function Render($params = null)
    {
//"urlbase" => "?path=orders
//"page" => &$VAR => return current page
//"ppg" => &$VAR  => return current ppv values

        $action_list = array(
            "swap"              => "Swap Items",
            "insert_after"      => "Insert After",
            "insert_before"     => "Insert Before",
            "sibling_delete"    => "Delete",
            "move_to_folder"    => "Move to folder"
        );

        $core = getCoreInstance();
        $framework = $core->getProperty("framework");
        $render = $framework->smarty;
        
        $al = $params['action_list'];
        $tmp_al = array();
        for($i = 0 ; $i < sizeof($al) ; $i++)
        {
            $tmp_al[$al[$i]] = $action_list[$al[$i]];
        }
        
        $select_text = "- - Select Action - -";
        
        $render->assign('insert_list_str',str2db($params['insert_list_str']));      
        $render->assign('res_folder_str',str2db($params['res_folder_str']));        
        
        $render->assign("action_list",$tmp_al);
        $render->assign("select_text",$select_text);
        $render->assign("action_url",$params['action_url']);

        $result = $render->fetch("file:".$this->tplfolder."/sibling.tpl.php");
        
        
        return $result;
    }
    
};


?>